<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no, width=device-width">
    <title>设备分布图</title>
    <link rel="stylesheet" href="http://cache.amap.com/lbs/static/main1119.css"/>
    <style>
        .info-tip {
            position: absolute;
            top: 10px;
            right: 10px;
            font-size: 12px;
            background-color: #fff;
            height: 35px;
            text-align: left;
        }
    </style>
    <script src="http://cache.amap.com/lbs/static/es5.min.js"></script>
    <script type="text/javascript" src="http://webapi.amap.com/maps?v=1.3&key=e2fb4affacae5cb70f3ac9f3c69c8e46&plugin=AMap.Geocoder"></script>
    <script type="text/javascript" src="http://cache.amap.com/lbs/static/addToolbar.js"></script>
</head>
<body>
<div id="container"></div>
<div class="button-group">
    <input id="setFitView" class="button" type="button" value="地图自适应显示"/>
</div>
<div class="info-tip">
    <div id="centerCoord"></div>
    <div id="tips"></div>
</div>
<script>
    var markers=[<c:forEach var="item" items="${allAddress}">"${item.location}",</c:forEach>];
    var positions= new Array();
    var map = new AMap.Map("container", {
        resizeEnable: true
    });
    function geocoder() {
        var geocoder = new AMap.Geocoder({
            city: "029", //城市，默认：“全国”
            radius: 1000 //范围，默认：500
        });
        //地理编码,返回地理编码结果
        for(var a = 0;a<markers.length ;a++){
            geocoder.getLocation(markers[a], function(status, result) {
                if (status === 'complete' && result.info === 'OK') {
                    geocoder_CallBack(result);
                    positions.forEach(function(position) {
                        var aa = position.split(",");
                        new AMap.Marker({
                            map: map,
                            position: [aa[0],aa[1]],
                            offset: new AMap.Pixel(-12, -36)
                        });
                    });
                    var center = map.getCenter();
                    var centerText = '当前中心点坐标：' + center.getLng() + ',' + center.getLat();

                    AMap.event.addDomListener(document.getElementById('setFitView'), 'click', function() {
                        var newCenter = map.setFitView();
                        document.getElementById('centerCoord').innerHTML = '当前中心点坐标：' + newCenter.getCenter();
                        document.getElementById('tips').innerHTML = '地图自适应显示到合适的范围内,点标记已全部显示在视野中！';
                    });

                }
            });
        };
    };
    //地理编码返回结果展示
    function geocoder_CallBack(data) {
        var geocode = data.geocodes;
        for (var i = 0; i < geocode.length; i++) {
            var position =  geocode[i].location.getLng() + "," + geocode[i].location.getLat();
            positions.push(position);
        }
    };
    geocoder();

</script>

</body>
</html>